module.exports = {
  loopAsync: (fn, interval) => {
    setImmediate(function next() {
      fn().then(() => {
        setTimeout(next, interval)
      }).catch(e => {
        console.log('failed to run %s', fn.name, e)
        setTimeout(next, interval)
      })
    })
  },
  loop: (fn, interval) => {
    setImmediate(function next() {
      try {
        fn()
      } catch (e) {
        console.log('failed to run %s', fn.name, e)
      }
      setTimeout(next, interval)
    })
  },
  sleep: (ms) => {
    return new Promise(resolve => {
      setTimeout(resolve, ms)
    })
  },
  prettyJson: (json => {
    return JSON.stringify(json, null, 2)
  })
}